#!groovy
// -*- mode: groovy -*-

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

// Jenkins pipeline
// See documents at https://jenkins.io/doc/book/pipeline/jenkinsfile/

// Docker env used for testing
// Different image may have different version tag
// because some of them are more stable than anoter.
//
// Docker images are maintained by PMC, cached in dockerhub
// and remains relatively stable over the time.
// Flow for upgrading docker env(need commiter)
//
// - Send PR to upgrade build script in the repo
// - Build the new docker image
// - Tag the docker image with a new version and push to a binary cache.
// - Update the version in the Jenkinsfile, send a PR
// - Fix any issues wrt to the new image version in the PR
// - Merge the PR and now we are in new version
// - Tag the new version as the lates
// - Periodically cleanup the old versions on local workers
//

// ============================= IMPORTANT NOTE =============================
// This file is generated by 'jenkins/generate.py'. Do not edit this file directly!
// Make edits to 'jenkins/Jenkinsfile.j2' and regenerate this with
// 'python3 jenkins/generate.py'
// Note: This timestamp is here to ensure that updates to the Jenkinsfile are
// always rebased on main before merging:
// Generated at {{ generated_time }}

import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
{% import 'ci/jenkins/macros.j2' as m with context -%}

// NOTE: these lines are scanned by docker/dev_common.sh. Please update the regex as needed. -->
ci_lint = 'tlcpack/ci-lint:20220715-060127-37f9d3c49'
ci_gpu = 'tlcpack/ci-gpu:20220801-060139-d332eb374'
ci_cpu = 'tlcpack/ci-cpu:20220715-060127-37f9d3c49'
ci_minimal = 'tlcpack/ci-minimal:20220725-133226-d3cefdaf1'
ci_wasm = 'tlcpack/ci-wasm:20220715-060127-37f9d3c49'
ci_i386 = 'tlcpack/ci-i386:20220715-060127-37f9d3c49'
ci_cortexm = 'tlcpack/ci-cortexm:v0.01'
ci_arm = 'tlcpack/ci-arm:20220715-060127-37f9d3c49'
ci_hexagon = 'tlcpack/ci-hexagon:20220715-060127-37f9d3c49'
// <--- End of regex-scanned config.

// Parameters to allow overriding (in Jenkins UI), the images
// to be used by a given build. When provided, they take precedence
// over default values above.
properties([
  parameters([
  {% for image in images %}
    string(name: '{{ image.name }}_param', defaultValue: ''),
  {% endfor %}
  ])
])

// Placeholders for newly built Docker image names (if rebuild_docker_images
// is used)
{% for image in images %}
  built_{{ image.name }} = null;
{% endfor %}

// Global variable assigned during Sanity Check that holds the sha1 which should be
// merged into the PR in all branches.
upstream_revision = null

// command to start a docker container
docker_run = 'docker/bash.sh --env CI --env TVM_SHARD_INDEX --env TVM_NUM_SHARDS --env RUN_DISPLAY_URL --env PLATFORM --env SKIP_SLOW_TESTS'
docker_build = 'docker/build.sh'
// timeout in minutes
max_time = 180
rebuild_docker_images = false

// Filenames for stashing between build and test steps
{% set tvm_runtime = ['build/libtvm_runtime.so', 'build/config.cmake'] %}
{% set tvm_lib = ['build/libtvm.so'] + tvm_runtime %}
{% set tvm_multilib = ['build/libtvm.so', 'build/libvta_fsim.so'] + tvm_runtime %}
{% set tvm_multilib_tsim = ['build/libvta_tsim.so'] + tvm_multilib %}
{% set microtvm_template_projects = ['build/microtvm_template_projects',] %}
{% set hexagon_api = ['build/hexagon_api_output',] %}
s3_prefix = "tvm-jenkins-artifacts-prod/tvm/${env.BRANCH_NAME}/${env.BUILD_NUMBER}"

{% set aws_default_region = "us-west-2" %}
{% set aws_ecr_url = "dkr.ecr." + aws_default_region + ".amazonaws.com" %}

// General note: Jenkins has limits on the size of a method (or top level code)
// that are pretty strict, so most usage of groovy methods in these templates
// are purely to satisfy the JVM
{% include "ci/jenkins/Prepare.groovy.j2" %}
{% include "ci/jenkins/DockerBuild.groovy.j2" %}
{% include "ci/jenkins/Lint.groovy.j2" %}
{% include "ci/jenkins/Build.groovy.j2" %}
{% include "ci/jenkins/Test.groovy.j2" %}
{% include "ci/jenkins/Deploy.groovy.j2" %}


cancel_previous_build()

prepare()

if (rebuild_docker_images) {
  build_docker_images()
}

lint()

build()

test()

deploy()
